
$(document).ready(function(){


    $( ".search" ).dialog({
        autoOpen: false,
        width: "auto"

    });

    $( ".search_button" ).click(function() {
        $( ".search" ).dialog( "open" );
        return false;
    });
    $( "tr.users" ).click(function() {
        $( ".users" ).css( "background-color","transparent" );
        $( this ).css( "background-color","#7fa6ff" );

    });


    $("#maincheck").click( function() {
        if($('#maincheck').attr('checked')){

            $('#sendmessage').css('display','inline');
            $('#ping').css('display','inline');

            $('.mc').attr('checked', true);

        } else {

            $('#sendmessage').css('display','none');
            $('#ping').css('display','none');
            $('.mc').attr('checked', false);
        }
    });


    $(".mc").click( function() {

        var n = $("input.mc:checked").length;

        if (n > 0)
        {
            $('#sendmessage').css('display','inline');
            $('#ping').css('display','inline');

        }
        else{

            $('#sendmessage').css('display','none');
            $('#ping').css('display','none');

        }





    });




    $( "#messagebox" ).dialog({
        autoOpen: false,
        width: "auto",
        modal: true

    });

    $("#sendmessage").click( function() {



        $( "#messagebox" ).dialog("open");

        $.ajax({

            url: 'users/delivery',

            success: function(data) {

                $('#messagebox').html(data);

                $("b").click( function() {

                    $('#message').append($(this).text());
                //alert($(this).text());


                });

                var fields = $(".mc").serializeArray();

                var users = '';

                jQuery.each(fields, function(i, field)
                {

                    users = field.value + "|" + users;

                });




                $("#messageform").validate(
                {

                    rules: {
                        "options[tariff]": "required",
                        title: {
                            required:true,
                            minlength:10
                        },
                        message: {
                            required:true,
                            minlength:1
                        },
                        "ckeck_type[]":{
                            required:true
                        }

                    },
                    
                    submitHandler: function(form) {
                        jQuery(form).ajaxSubmit({
                            data: {
                                users:users
                            },
                            beforeSubmit:  function(arr, $form, options) {
                                $('img').css('display','inline')
                            },
                            success:        function(arr, $form, options) {
                                $('#messagebox').html('Message send succeful!');
                            }

                        });
                    }

                });



                $("textarea[id='message']").keyup(function countRemainingChars(){
                    maxchars = 70;
                    number = $("textarea[id='message']").val().length;
                    // if(number <= maxchars){
                    $("#countblock").html("Для SMS: <b>" + (maxchars-number) + "</b> символів залишилось");
                    // }
                    if(number == maxchars) {
                //$("#message").attr({ maxlength: maxchars});
                }
                });





            }

        });


    });



    $( "#pingsimple" ).dialog({
        autoOpen: false,
        width: "auto",
        modal: false

    });

    $( "#pingnmap" ).dialog({
        autoOpen: false,
        width: "auto",
        modal: false

    });


    $("#ping").click( function() {

        $( "#pingsimple" ).dialog("open");
    });

    $("#nmap").click( function() {

        $( "#pingnmap" ).dialog("open");
    });




    var options = {
        target:        '#pingresult',
        beforeSubmit:  function(arr, $form, options) {
            $('#sping_r img').css('display','inline')
        },
        success:        function(arr, $form, options) {
            $('#sping_r img').css('display','none')
        }

    };
    $('#pingform').ajaxForm(options);

    var options = {
        target:        '#nmapresult',
        beforeSubmit:  function(arr, $form, options) {
            $('#nmap_r img').css('display','inline')
        },
        success:        function(arr, $form, options) {
            $('#nmap_r img').css('display','none')
        }

    };
    $('#nmapform').ajaxForm(options);






    $( "#freezing" ).dialog({
        autoOpen: false,
        width: "500px",
        modal: false

    });


    $("#freez").click( function() {

        $( "#freezing" ).dialog("open");

        $.ajax({

            url: '../../users/freezing/' + $('#login').html(),

            success: function(data) {

                $('#freezing').html(data);


                $(function() {
                    var dates = $('#from, #to').datepicker({
                        defaultDate: "-1m",
                        changeMonth: true,
                        changeYear: true,
                        dateFormat: 'yy-mm-dd',
                        firstDay: 1 ,
                        numberOfMonths: 3,
                        onSelect: function(selectedDate) {
                            var option = this.id == "from" ? "minDate" : "maxDate";
                            var instance = $(this).data("datepicker");
                            var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                            dates.not(this).datepicker("option", option, date);
                        }
                    });
                });


            }
        });

    });


    $( "#taskbox" ).dialog({
        autoOpen: false,
        width: "500px",
        modal: false

    });

    $("#tasks").click( function() {

        $( "#taskbox" ).dialog("open");

        $.ajax({

            url: '../../users/tasks/' + $('#login').html(),

            success: function(data) {

                $('#taskbox').html(data);


            }
        });

    });




    $("#balance").validate(
    {
        rules: {

            "options[cash]":
            {
                required:true,
                number: true
            },
                            
            paytype: "required"
                        
        }
    });
    $("#tariff").validate(
    {
        rules: {
            "options[tariff]": "required"
        }

    });
        $(function() {
            $.validator.addMethod('IP4Checker', function(value) {
                var ip = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
                return value.match(ip);
            }, 'Invalid IP address');
            
        $("#userform").validate(
        {
                   
            rules: {
                "options[ip]": {
                    required:true,
                    IP4Checker:true
                },
                "options[password]":{
                    required:true,
                    minlength:6
                    
                },
                "options[name]":"required"
                
            }

        });
    });
  
    $('input:checkbox.check').attr('disabled', true);
    $("#hideshow").click( function() {
        if($('#hideshow').attr('checked')){

            $('.noshow').css('display','inline');
            $('.show').css('display','none');
            $('input:checkbox.check').removeAttr('disabled');
        } else {

            $('.noshow').css('display','none');
            $('.show').css('display','inline');
            $('input:checkbox.check').attr('disabled', true);
        }
    });

    $("input:[name=options\[phone\]]").mask("9-99-99");
    $("input:[name=options\[userdata1\]]").mask("(999)999-99-99");
    //$("#created").mask("9999-99-99");
    $("input:[name=options\[userdata0\]]").mask("**:**:**:**:**");
    $('#created').datepicker({
        defaultDate: "-1m",
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
        firstDay: 1 ,
        numberOfMonths: 3
    });
});

function popup(url,name,w,h)
{
         

    var wc=parseInt((screen.availWidth-w)/2)
    var hc=parseInt((screen.availHeight-h)/2)
    window.open(url,name,'width='+w+',height='+h+',top='+hc+',screenY='+hc+',left='+wc+',screenX='+wc+'toolbar=no, scrollbars=no,resizable=no,status=no')
}
